
package models.spider.deprecated.rdf;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import play.Logger;

/**
 * 
 * @author laurent@opprecht.info
 */
public class Builder {

    public Document parse(final String content) {
        try {
            UriAdapter uriAdapter = new UriAdapter();

            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xReader = xif.createXMLStreamReader(new StringReader(content.trim()));
            xReader = xif.createFilteredReader(xReader, uriAdapter);

            JAXBContext context = JAXBContext.newInstance(Document.class); // parse
                                                                           // the
                                                                           // XML
                                                                           // and
                                                                           // return
                                                                           // an
                                                                           // instance
                                                                           // of
                                                                           // the
                                                                           // AppConfig
                                                                           // class
            Unmarshaller unmarshaller = context.createUnmarshaller();
            unmarshaller.setAdapter(uriAdapter);
            Document result = (Document) unmarshaller.unmarshal(xReader);
            return result;
        } catch (Exception e) {
            Logger.error("Parsing failed", e);
            return null;
        }
    }
}
